#include<iostream>
#include<ros/ros.h>
#include<powermanagement_msg/temprature.h>
#include<std_msgs/Bool.h>
using namespace std;
struct human
{
    float max;
    int pos;
};

int main(int argc,char * argv[])
{
    ros::init(argc,argv,"termoHumanDetection");
    ros::NodeHandle nh;
    float heat_avg;
    ros::Publisher human_pub=nh.advertise<std_msgs::Bool>("/is_human_in_front",10);
    float dist[8];

    std_msgs::Bool pub_msg;

    {
        powermanagement_msg::tempratureConstPtr msg= ros::topic::waitForMessage<powermanagement_msg::temprature>("/robina_dashBoard/powermanagement",nh);
        float sum=0;
        for(int i=0;i<8;i++)
        {
            sum+=msg->front_temprature[i];
        }
        heat_avg=sum/8.0f;
    }

    while(ros::ok())
    {
        powermanagement_msg::tempratureConstPtr msg= ros::topic::waitForMessage<powermanagement_msg::temprature>("/robina_dashBoard/powermanagement",nh);
        if(msg.get()==NULL)
            continue;
//        float sum=0;
//        for(int i=0;i<8;i++)
//        {

//            sum+=msg->rear_temprature[i];
//        }


//        for(int i=7;i>=0;i--)
//        {
//            avg[i]=(sum-msg->front_temprature[i])/7-msg->front_temprature[i];

//        }
        for(int i=7;i>=0;i--)
        {
            dist[i]=msg->front_temprature[i]-heat_avg;
        }

        float max=dist[0];
        for(int i=7;i>=0;i--)
        {
            if(max<dist[i])
                max=dist[i];
        }


        //cout<<max;
        if(max>=3)
        {
            //cout<<"human found";
            pub_msg.data=true;
        }
        else
        {
            //cout<<"\t\tHuman not found";
            pub_msg.data=false;
        }
        human_pub.publish(pub_msg);
        //cout<<endl;


    }

}
